<?php
/**
 * Base.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: 2019/5/5
 * Time: 14:08
 */
namespace app\console\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

abstract class Base extends Command {
    protected function configure()
    {
        $this->applyOption();
        // php think test param1 param2 --action xxx --options1 xxx --options2 xxx
        $this->setName($this->getCmdName())->setDescription($this->getCmdDescription())
            ->addArgument('argv',Argument::IS_ARRAY,'param array')
            // VALUE_REQUIRED 是必填的选项,VALUE_OPTIONAL 是可选
            ->addOption('action',null,Option::VALUE_REQUIRED,'Different collection categories');
        // ->addOption('options1',null,Option::VALUE_REQUIRED,'options1')
        // ->addOption('options2',null,Option::VALUE_OPTIONAL,'options2')
    }
    protected function execute(Input $input, Output $output)
    {
        $param = $input->getArguments();
        $option = $input->getOptions();
        if(empty($option['action'])){
            exit("action required");
        }
        $action = $option['action'];
        if(method_exists($this,$action)){
            $this->processResult($this->$action($param['argv'],$option));
            // 正常退出
            exit;
        }else{
            exit("action error");
        }
    }
    protected function applyOption(){}
    abstract protected function getCmdName();
    abstract protected function getCmdDescription();
    abstract protected function processResult($result);
}